package com.cmbchina.encrypt;

/**
 * @author liuxb
 */
public class Checker {
    public Checker() {
    }

    public static <T> T notNull(T t) throws SMCryptKYException {
        if (t == null) {
            throw new SMCryptKYException(ErrorCode.E10400);
        } else {
            return t;
        }
    }

    public static byte[] tooSmall(byte[] b) throws SMCryptKYException {
        if (b.length == 0) {
            throw new SMCryptKYException(ErrorCode.E10415);
        } else {
            return b;
        }
    }

    public static void check(boolean b, ErrorCode code) throws SMCryptKYException {
        if (!b) {
            throw new SMCryptKYException(code);
        }
    }

    public static void check(boolean b, ErrorCode code, String message) throws SMCryptKYException {
        if (!b) {
            throw new SMCryptKYException(code, message);
        }
    }

    public static void check(boolean b, ErrorCode code, String format, Object... args) throws SMCryptKYException {
        if (!b) {
            throw new SMCryptKYException(code, String.format(format, args));
        }
    }

    public static void checkData(String data) throws SMCryptKYException {
        notNull(data);
        if (data.length() == 0) {
            throw new SMCryptKYException(ErrorCode.E10413);
        }
    }

    public static void checkData(byte[] data) throws SMCryptKYException {
        notNull(data);
        if (data.length == 0) {
            throw new SMCryptKYException(ErrorCode.E10413);
        }
    }

    public static void checkArgument(boolean b, String message) {
        if (!b) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void checkArgument(boolean b, String format, Object... args) {
        if (!b) {
            throw new IllegalArgumentException(String.format(format, args));
        }
    }
}